# Contributor: mio <miyopan@e.email>
# Maintainer: mio <miyopan@e.email>
pkgname=tic-80
pkgver=1.0.2164
_gitrev="b09c50c61f2e8f66959ee9539e5a05feaeaf8ae1"
pkgrel=0
pkgdesc="Fantasy computer for making, playing and sharing tiny games"
url="https://tic80.com/"
license="MIT"
arch="all !ppc64le" # Build failed on ppc64le with libpng.a error
depends="
	fennel5.3
	lua5.3
	lua5.3-moonscript
	ruby
	ruby-rake
	"
makedepends="
	cmake
	fennel5.3
	freeglut-dev
	giflib-dev
	glu-dev
	http-parser-dev
	jack-dev
	libpng-dev
	libuv-dev
	lua5.3-dev
	lua5.3-moonscript
	pulseaudio-dev
	ruby-dev
	ruby-rake
	samurai
	sdl2-dev
	zlib-dev
	"
builddir="$srcdir"/TIC-80-"$pkgver"
options="!check" # No tests

# Submodules list from https://github.com/nesbox/TIC-80/tree/v$pkgver/vendor
# The zip library used is a minizip fork that is not a direct replacement.
# The moonscript library contains a required header file.
# Sokol is not enabled by default, so it is not included here as a dependency.
_argparse_rev="0d5f5d0745df14a3f373f7eed85bf524714f4524"
_blip_buf_rev="330226d9b55ecbeea644e17b5e0f096a165ca07e"
_circle_stdlib_rev="fdb3c4a948421d47fddab8042a92f980cba43915"
_dirent_rev="c885633e126a3a949ec0497273ec13e2c03e862c"
_duktape_rev="28f915a5381ca3a7dfa22130d08fbc5a16ae5d2f"
_lpeg_rev="73d8614a8dea404cf7bfe25a6e4cea7183dc9fb7"
_moonscript_rev="17179283012b392bff972ad66231d73bfeec6e90"
_mruby_rev="0f45836b5954accf508f333f932741b925214471"
_squirrel_rev="9dcf74f99097898dd5a111c4a55b89d1c4d606c0"
_wasm3_rev="a3abb3f2d320994be59dfbe2cdafa8d0b6a8253e"
_wren_rev="4a18fc489f9ea3d253b20dd40f4cdad0d6bb40eb"
_zip_rev="d7df626f3aa457e01669f65e61bf8f484e352941"
_submodules="
	argparse-$_argparse_rev
	blip-buf-$_blip_buf_rev
	circle-stdlib-$_circle_stdlib_rev
	dirent-$_dirent_rev
	duktape-releases-$_duktape_rev
	lpeg-$_lpeg_rev
	moonscript-$_moonscript_rev
	mruby-$_mruby_rev
	squirrel-$_squirrel_rev
	wasm3-$_wasm3_rev
	wren-$_wren_rev
	zip-$_zip_rev
	"
source="tic-80-$pkgver.tar.gz::https://github.com/nesbox/TIC-80/archive/v$pkgver/tic80-v$pkgver.tar.gz
	argparse-$_argparse_rev.tar.gz::https://github.com/cofyc/argparse/archive/$_argparse_rev.tar.gz
	blip-buf-$_blip_buf_rev.tar.gz::https://github.com/nesbox/blip-buf/archive/$_blip_buf_rev.tar.gz
	circle-stdlib-$_circle_stdlib_rev.tar.gz::https://github.com/smuehlst/circle-stdlib/archive/$_circle_stdlib_rev.tar.gz
	dirent-$_dirent_rev.tar.gz::https://github.com/tronkko/dirent/archive/$_dirent_rev.tar.gz
	duktape-$_duktape_rev.tar.gz::https://github.com/svaarala/duktape-releases/archive/$_duktape_rev.tar.gz
	lpeg-$_lpeg_rev.tar.gz::https://github.com/nesbox/lpeg/archive/$_lpeg_rev.tar.gz
	moonscript-$_moonscript_rev.tar.gz::https://github.com/nesbox/moonscript/archive/$_moonscript_rev.tar.gz
	mruby-$_mruby_rev.tar.gz::https://github.com/mruby/mruby/archive/$_mruby_rev.tar.gz
	squirrel-$_squirrel_rev.tar.gz::https://github.com/albertodemichelis/squirrel/archive/$_squirrel_rev.tar.gz
	wasm3-$_wasm3_rev.tar.gz::https://github.com/wasm3/wasm3/archive/$_wasm3_rev.tar.gz
	wren-$_wren_rev.tar.gz::https://github.com/wren-lang/wren/archive/$_wren_rev.tar.gz
	zip-$_zip_rev.tar.gz::https://github.com/kuba--/zip/archive/$_zip_rev.tar.gz
	use-repo-deps.patch
	"

prepare() {
	default_prepare
	cd "$srcdir"

	# Copy extracted submodules into the main package source
	_vendor_dir="$srcdir"/TIC-80-"$pkgver"/vendor/
	for _mod in $_submodules; do
		_mod_rev=${_mod##*-}
		_mod_name=$(echo $_mod | sed -e "s/-$_mod_rev//")
		case $_mod in
		duktape-releases*)
			mv $_mod duktape
			mv duktape $_vendor_dir
			;;
		*)
			mv $_mod $_mod_name
			mv $_mod_name $_vendor_dir
			;;
		esac
	done

	# Set version revision for executable to display the correct version
	_version_rev=${pkgver##*\.}
	sed -i "s/VERSION_REVISION 0/VERSION_REVISION $_version_rev/" \
		TIC-80-"$pkgver"/CMakeLists.txt
}

build() {
	cmake -B build-tic -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DVERSION_HASH=${_gitrev:0:7} \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DBUILD_PRO=ON
	cmake --build build-tic
}

package() {
	DESTDIR="$pkgdir" cmake --install build-tic

	# extra tools need manual install
	cd build-tic
	install -Dm755 bin/bin2txt -t "$pkgdir"/usr/bin
	install -Dm755 bin/cart2prj -t "$pkgdir"/usr/bin
	install -Dm755 bin/player-sdl -t "$pkgdir"/usr/bin
	install -Dm755 bin/prj2cart -t "$pkgdir"/usr/bin
	install -Dm755 bin/wasmp2cart -t "$pkgdir"/usr/bin
	install -Dm755 bin/xplode -t "$pkgdir"/usr/bin
}

sha512sums="
c4765d00ad5dcbcf14e78eaa4e42b0128a5010c093bfacd79dbfc49fe951dc06ee4dab412c29662d70832b0298aa721aa2c31438db23fbd225790091f42b6005  tic-80-1.0.2164.tar.gz
2dba667249c19ec470421327f6cc20f11a168847a99f60db395980c28339a269041a778645355d8ecbac76e1eac0b77c12f3f2fb3573e0e372ccfffdc947ca1c  argparse-0d5f5d0745df14a3f373f7eed85bf524714f4524.tar.gz
69c42cd93cbb640952244c44cdaf75e0808536ca37300912d026c2aedc878b5f105ae109a5eef1a558b1fdc8a692112491515c6813113a007878908224eba5d8  blip-buf-330226d9b55ecbeea644e17b5e0f096a165ca07e.tar.gz
f12f89b95913ab3b3a3f23ccabf93a4591875a1dda5ac50f9318d3e750b6f9fcc2450bd2172592b14ae2734d96b3c3986c488bb3d0190213d7f439121533035a  circle-stdlib-fdb3c4a948421d47fddab8042a92f980cba43915.tar.gz
ce4a2b445073b3bb82d31934ce2d1dcea771723d4cf1778e065cfe787e91128d995cfccc96d860aceb3637f35627dfbf597e423967f08c5ecf855992e9a84581  dirent-c885633e126a3a949ec0497273ec13e2c03e862c.tar.gz
c0af8ec295cb86371dc1ba74b446d0abe5b6ea51fb9ef68b01aea08d066d1c8b4fea4b6ce1e7786076bb19135bd1677a80f7ab889efddb43c5ac39d5a9ecea38  duktape-28f915a5381ca3a7dfa22130d08fbc5a16ae5d2f.tar.gz
93956abcafe84d200fab41d54686cbba6ac8eae91292dc4282186543edef8211e2768dab83854b149e21cb0775b1c2ef8fd79b94f3f72a860b9710841c843b2e  lpeg-73d8614a8dea404cf7bfe25a6e4cea7183dc9fb7.tar.gz
05a26627c1a8686d876654751e590c024aa603fff6ff75ed4ed61d499c16c9fef5616222a6dfc5f11aaf9a7ed79786f92e5a15ca8a42fa79c5adba3228f3bdad  moonscript-17179283012b392bff972ad66231d73bfeec6e90.tar.gz
699c3bbbcbbe51806d1c5802c623bebf0b1d33e80052ea2c0ef7d51b1e9d0bb17e3469613272832a7defd9c344ed44960e8d0f32a59808ea06fff23b3e7360eb  mruby-0f45836b5954accf508f333f932741b925214471.tar.gz
0bc2e0842b429854a8f61f2ed0b2261f6af49849f1a90f2fc2aa3578a892b2a97131dcc209eb9c7ceda5544162542a757d4ca091797f0fc0324664f3cfe7fc7e  squirrel-9dcf74f99097898dd5a111c4a55b89d1c4d606c0.tar.gz
e23e24b697c068b27a5aba71dec33d84b5079155344aa52d2997e2267986167df30ee3e264ad8ff40c62ae73b95eb48896b9e8a47bf9904cc23bbae61818e245  wasm3-a3abb3f2d320994be59dfbe2cdafa8d0b6a8253e.tar.gz
b3d79e9cb647e52db06d5cddfc8c93d05ae0e8d87f0f879ac2b812fcc7f55e018d21d3b04d62eaeb12e6d931b5e43fbe357b187e7f446e86e39be015c51c2eee  wren-4a18fc489f9ea3d253b20dd40f4cdad0d6bb40eb.tar.gz
ebfa73418963010277f112e1e652c8da54994087a1d8095c527f75a799e4d0933e4ff78bdb361cedb1c844b167145fe010c2805a08c2825476f755899eb68fdc  zip-d7df626f3aa457e01669f65e61bf8f484e352941.tar.gz
885eb325d0202356c3991f63dbf45c841914d2daa67c4a9db0061890ce7e959fb1a4b35c230987eaca8f6adc800070393657ac4a7a46b8dc6e94a70ffaa3ab6a  use-repo-deps.patch
"
